<template>
  <div class="login-container">
    <el-card>
      <div class="avatar_box">
        <img src="@/assets/img/heima.png" alt="" />
      </div>
      <el-form
        ref="ruleForm"
        :model="loginForm"
        :rules="loginRules"
        :hide-required-asterisk="true"
        label-position="left"
      >
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" />
          <template #label>
            <i class="el-icon-s-custom" />
          </template>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" />
          <template #label>
            <i class="el-icon-lock" />
          </template>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button class="resetBtn" @click="resetForm('ruleForm')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入名称' },
          { min: 3, max: 6, trigger: 'blur', message: '用户名长度为3-6个字符' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入密码' },
          {
            min: 6,
            max: 16,
            trigger: 'blur',
            message: '密码长度为6-16 个字符'
          }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  methods: {
    ...mapActions(['user/login']),
    submitForm () {
      this.$refs.ruleForm.validate(async (isOk) => {
        if (isOk) {
          try {
            this.loading = true
            await this['user/login'](this.loginForm)
            this.$router.push('/home')
            this.$message.success('登录成功')
          } catch (err) {
            console.log(err)
          } finally {
            this.loading = false
          }
        } else {
          this.$message.success('请检查账号或密码是否正确')
        }
      })
    },
    resetForm (ruleForm) {
      this.$refs[ruleForm].resetFields()
    }
  }
}
</script>

<style lang="less">
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #2b4b6b;
  overflow: hidden;
  .el-card {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    overflow: unset;
    .el-card__body {
      padding: 0;
    }
    .avatar_box {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      border: 1px solid #eee;
      background-color: #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      box-sizing: content-box;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
  .el-form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }

  .el-form-item {
    margin-bottom: 22px;
    .el-form-item__label {
      position: absolute;
      left: 25px;
      font-size: 16px;
      z-index: 999;
      color: #c0c4cc;
    }
    .el-input__inner {
      padding-left: 30px;
      color: #606266;
    }
  }
  .btns {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 22px;
    .resetBtn {
      color: #fff;
      background-color: #909399;
      border-color: #909399;
    }
  }
}
</style>
